package com.viethp.mt.activities;

import com.viethp.mt.activities.R;
import com.viethp.mt.common.CommonHelper;
import com.viethp.mt.common.MyLocationListener;
import com.viethp.mt.customactivities.ATabActivityGroup;
import com.viethp.mt.customactivities.ATabNearByGroup;
import com.viethp.mt.objects.Position;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

public class AMain extends TabActivity {
	/** Called when the activity is first created. */

	private TabHost tabHost;
	private int currentTab;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		setTabs();

		currentTab = getIntent().getIntExtra("CURRENT_TAB", 0);
		tabHost.setCurrentTab(currentTab);
		// Intent extras = getIntent();
		// String user_id = extras.getStringExtra("user_id");
		// Log.d("userid", user_id);
		setPosition();
	}

	private void setTabs() {
		addTab("Activity", R.drawable.btn_tab_activity, ATabActivityGroup.class);
		addTab("Nearby", R.drawable.btn_tab_nearby, ATabNearByGroup.class);
		addTab("I Have", R.drawable.btn_tab_have, APostItemDetails.class);
		addTab("Account", R.drawable.btn_tab_account, ATabAccount.class);

	}

	private void addTab(String labelId, int drawableId, Class<?> c) {
		tabHost = getTabHost();
		Intent intent = new Intent(this, c);
		TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);

		View tabIndicator = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, getTabWidget(), false);
		TextView title = (TextView) tabIndicator.findViewById(R.id.title);
		title.setText(labelId);
		ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
		icon.setImageResource(drawableId);

		spec.setIndicator(tabIndicator);
		spec.setContent(intent);
		tabHost.addTab(spec);
	}

	// position
	public void setPosition() {
		LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		Location loc = mlocManager
				.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		LocationListener mlocListener = new MyLocationListener(AMain.this);

		mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
				mlocListener);
		if (loc != null) {
			Position pos = new Position();
			pos.setLon(String.valueOf(loc.getLongitude()));
			pos.setLat(String.valueOf(loc.getLatitude()));
			CommonHelper.setPosition(pos, this);
			// CommonHelper.showToast(this, ""
			// + CommonHelper.getPosition(this).getLon() + " "
			// + CommonHelper.getPosition(this).getLat());
			Log.d("LastKnowLocation", ""
					+ CommonHelper.getPosition(this).getLon() + " "
					+ CommonHelper.getPosition(this).getLat());
		}
		// else {
		// Position pos = new Position();
		// pos.setLon(String.valueOf(GlobalVariables.lon));
		// pos.setLat(String.valueOf(GlobalVariables.lat));
		// CommonHelper.setPosition(pos, this);
		// CommonHelper.showToast(this, ""
		// + CommonHelper.getPosition(this).getLon() + " "
		// + CommonHelper.getPosition(this).getLat());
		// }
	}
}